package com.sztx.pay.center.core.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sztx.pay.center.core.service.RiskControlService;
import com.sztx.pay.center.dataaccess.dao.mysql.RiskControlRecordMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.RiskControlRuleMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.RiskControlRulePersonalMysqlDAO;
import com.sztx.pay.center.dataaccess.dao.mysql.RiskWithdrawBlackWhiteListMysqlDAO;
import com.sztx.se.core.service.impl.BaseServiceImpl;

@Service
public class RiskControlServiceImpl extends BaseServiceImpl implements RiskControlService {

	@Autowired
	private RiskControlRulePersonalMysqlDAO personalDao;

	@Autowired
	private RiskControlRuleMysqlDAO ruleDao;

	@Autowired
	private RiskControlRecordMysqlDAO recordDao;

	@Autowired
	private RiskWithdrawBlackWhiteListMysqlDAO riskWithDrawAuthDao;

//	@Override
//	public void check(String userId, UserType userType, TranType tradeType, long amount, boolean isNoUsePwd) {
//		// 参数验证
//		ValidateUtil.isNotBlank(userId, "用户账号不能为空");
//		ValidateUtil.notNull(userType, "用户类型不能为空");
//		ValidateUtil.notNull(tradeType, "交易类型不能为空");
//		ValidateUtil.gt((int) amount, 0, "金额不能小于等于0");
//
//		RiskControlRuleDO rule = null;
//
//		// 检测提现权限
//		if (tradeType == TranType.WITHDRAW_BERBON_ALIPAY || tradeType == TranType.WITHDRAW_BERBON_BANKCARD || tradeType == TranType.TRANSFER_BANKCARD_BANKCARD
//				|| tradeType == TranType.TRANSFER_BERBON_BANKCARD) {
//			RiskWithdrawBlackWhiteListDO blackWhiteListDO = riskWithDrawAuthDao.findByUser(userId);
//			if (blackWhiteListDO == null) {
//				throw new BusinessException("该帐号无提现权限,无法进行代发");
//			}
//			Byte status = blackWhiteListDO.getStatus();
//			Byte type = blackWhiteListDO.getType();
//			if (status.intValue() != 1) {
//				throw new BusinessException("该帐号无提现权限,无法进行代发");
//			}
//			if (type.intValue() != 1) {
//				throw new BusinessException("该帐号为提现黑名单帐号,无法进行代发");
//			}
//		}
//
//		// b2c 交易 转账 校验免密支付
//		if (isNoUsePwd) {
//			TradeOrderType tradeOrderType = null;
//			if (tradeType == TranType.PURCHASE) {
//				tradeOrderType = TradeOrderType.B2C_TRADE_ORDER;
//			} else if (tradeType == TranType.TRANSFER_BANKCARD_BANKCARD || tradeType == TranType.TRANSFER_BANKCARD_BERBON
//					|| tradeType == TranType.TRANSFER_BERBON_BANKCARD || tradeType == TranType.TRANSFER_BERBON_BERBON) {
//				tradeOrderType = TradeOrderType.TRANSFER_ORDER;
//			} else {
//				throw new BusinessException("该支付方式不支持免密支付");
//			}
//			noPwdPayService.checkIsAllow(userId, tradeOrderType, (int) amount);
//		}
//
//		// 查询个人风控规则
//		RiskControlRulePersonalDO personalRule = personalDao.getByUser(userId);
//
//		// 按照用户类型查找风控规则
//		if (personalRule == null) {
//			rule = ruleDao.getByUserType(userType.getCode());
//		} else {
//			rule = personalRule;
//		}
//
//		// 查找风控记录
//		RiskControlRecordDO record = recordDao.getByUser(userId);
//		if (record == null) {
//			record = new RiskControlRecordDO();
//			record.setUserId(userId);
//			record.setCurrentDay(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
//			record.setCurrentMonth(Calendar.getInstance().get(Calendar.MONTH) + 1);
//		} else {
//			if (record.getCurrentDay() != Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
//				record.setTradeDayAmount(0);
//				record.setTradeDayNumber(0);
//				record.setChargeDayAmount(0);
//				record.setChargeDayNumber(0);
//				record.setTransferDayAmount(0);
//				record.setTransferDayNumber(0);
//				record.setWithdrawDayAmount(0);
//				record.setWithdrawDayNumber(0);
//			}
//		}
//
//		// 是否通过风控
//		if (rule == null) {
//			// 无风控规则，则直接通过 TODO
//			return;
//		} else {
//			// 风控检查
//			switch (tradeType.getValue()) {
//			case 10:// TranType.CHARGE
//				if (rule.getChargeDayNumberLimit() >= 0) {
//					ValidateUtil.le(record.getChargeDayNumber() + 1, rule.getChargeDayNumberLimit(),
//							String.format("单日充值次数不能超过%s次", rule.getChargeDayNumberLimit()));
//				}
//				if (rule.getChargeOnceAmountLower() >= 0) {
//					ValidateUtil.ge((int) amount, (int) rule.getChargeOnceAmountLower(),
//							String.format("单笔充值金额最低%s元", new DecimalFormat("0.00").format((double) rule.getChargeOnceAmountLower() / 100)));
//				}
//				if (rule.getChargeOnceAmountUpper() >= 0) {
//					ValidateUtil.le((int) amount, (int) rule.getChargeOnceAmountUpper(),
//							String.format("单笔充值金额最高%s元", new DecimalFormat("0.00").format((double) rule.getChargeOnceAmountUpper() / 100)));
//				}
//				if (rule.getChargeDayAmountUpper() >= 0) {
//					ValidateUtil.le((int) (record.getChargeDayAmount() + amount), (int) rule.getChargeDayAmountUpper(),
//							String.format("每日充值金额不能超过%s元", new DecimalFormat("0.00").format((double) rule.getChargeDayAmountUpper() / 100)));
//				}
//				if (rule.getChargeMonthAmountUpper() >= 0) {
//					ValidateUtil.le((int) (record.getChargeMonthAmount() + amount), (int) rule.getChargeMonthAmountUpper(),
//							String.format("每月充值金额不能超过%s元", new DecimalFormat("0.00").format((double) rule.getChargeMonthAmountUpper() / 100)));
//				}
//				break;
//			case 1:// TranType.PURCHASE
//				if (rule.getTradeDayNumberLimit() >= 0) {
//					ValidateUtil.le(record.getTradeDayNumber() + 1, rule.getTradeDayNumberLimit(),
//							String.format("单日交易次数不能超过%s次", rule.getTradeDayNumberLimit()));
//				}
//				if (rule.getTradeOnceAmountLower() >= 0) {
//					ValidateUtil.ge((int) amount, (int) rule.getTradeOnceAmountLower(),
//							String.format("单笔交易金额最低%s元", new DecimalFormat("0.00").format((double) rule.getTradeOnceAmountLower() / 100)));
//				}
//				if (rule.getTradeOnceAmountUpper() >= 0) {
//					ValidateUtil.le((int) amount, (int) rule.getTradeOnceAmountUpper(),
//							String.format("单笔交易金额最高%s元", new DecimalFormat("0.00").format((double) rule.getTradeOnceAmountUpper() / 100)));
//				}
//				if (rule.getTradeDayAmountUpper() >= 0) {
//					ValidateUtil.le((int) (record.getTradeDayAmount() + amount), (int) rule.getTradeDayAmountUpper(),
//							String.format("每日交易金额不能超过%s元", new DecimalFormat("0.00").format((double) rule.getTradeDayAmountUpper() / 100)));
//				}
//				if (rule.getTradeMonthAmountUpper() >= 0) {
//					ValidateUtil.le((int) (record.getTradeMonthAmount() + amount), (int) rule.getTradeMonthAmountUpper(),
//							String.format("每月交易金额不能超过%s元", new DecimalFormat("0.00").format((double) rule.getTradeMonthAmountUpper() / 100)));
//				}
//				break;
//			case 30:// TranType.TRANSFER
//			case 31:
//			case 32:
//			case 33:
//				if (rule.getTransferDayNumberLimit() >= 0) {
//					ValidateUtil.le(record.getTransferDayNumber() + 1, rule.getTransferDayNumberLimit(),
//							String.format("单日转账次数不能超过%s次", rule.getTransferDayNumberLimit()));
//				}
//				if (rule.getTransferOnceAmountLower() >= 0) {
//					ValidateUtil.ge((int) amount, (int) rule.getTransferOnceAmountLower(),
//							String.format("单笔转账金额最低%s元", new DecimalFormat("0.00").format((double) rule.getTransferOnceAmountLower() / 100)));
//				}
//				if (rule.getTransferOnceAmountUpper() >= 0) {
//					ValidateUtil.le((int) amount, (int) rule.getTransferOnceAmountUpper(),
//							String.format("单笔转账金额最高%s元", new DecimalFormat("0.00").format((double) rule.getTransferOnceAmountUpper() / 100)));
//				}
//				if (rule.getTransferDayAmountUpper() >= 0) {
//					ValidateUtil.le((int) (record.getTransferDayAmount() + amount), (int) rule.getTransferDayAmountUpper(),
//							String.format("每日转账金额不能超过%s元", new DecimalFormat("0.00").format((double) rule.getTransferDayAmountUpper() / 100)));
//				}
//				if (rule.getTransferMonthAmountUpper() >= 0) {
//					ValidateUtil.le((int) (record.getTransferMonthAmount() + amount), (int) rule.getTransferMonthAmountUpper(),
//							String.format("每月转账金额不能超过%s元", new DecimalFormat("0.00").format((double) rule.getTransferMonthAmountUpper() / 100)));
//				}
//				break;
//			case 20:// TranType.WITHDRAW
//			case 21:
//			case 22:
//			case 23:
//				if (rule.getWithdrawDayNumberLimit() >= 0) {
//					ValidateUtil.le(record.getWithdrawDayNumber() + 1, rule.getWithdrawDayNumberLimit(),
//							String.format("单日提现次数不能超过%s次", rule.getWithdrawDayNumberLimit()));
//				}
//				if (rule.getWithdrawOnceAmountLower() >= 0) {
//					ValidateUtil.ge((int) amount, (int) rule.getWithdrawOnceAmountLower(),
//							String.format("单笔提现金额最低%s元", new DecimalFormat("0.00").format((double) rule.getWithdrawOnceAmountLower() / 100)));
//				}
//				if (rule.getWithdrawOnceAmountUpper() >= 0) {
//					ValidateUtil.le((int) amount, (int) rule.getWithdrawOnceAmountUpper(),
//							String.format("单笔提现金额最高%s元", new DecimalFormat("0.00").format((double) rule.getWithdrawOnceAmountUpper() / 100)));
//				}
//				if (rule.getWithdrawDayAmountUpper() >= 0) {
//					ValidateUtil.le((int) (record.getWithdrawDayAmount() + amount), (int) rule.getWithdrawDayAmountUpper(),
//							String.format("每日提现金额不能超过%s元", new DecimalFormat("0.00").format((double) rule.getWithdrawDayAmountUpper() / 100)));
//				}
//				if (rule.getWithdrawMonthAmountUpper() >= 0) {
//					ValidateUtil.le((int) (record.getWithdrawMonthAmount() + amount), (int) rule.getWithdrawMonthAmountUpper(),
//							String.format("每月提现金额不能超过%s元", new DecimalFormat("0.00").format((double) rule.getWithdrawMonthAmountUpper() / 100)));
//				}
//				break;
//			default:
//				break;
//			}
//		}
//	}
//
//	@Override
//	public void updateUserRiskData(String userId, TranType tradeType, long amount, boolean isNoUsePwd) {
//
//		// b2c 交易 转账 免密支付 累计交易金额更新
//		if (isNoUsePwd) {
//			TradeOrderType tradeOrderType = null;
//			if (tradeType == TranType.PURCHASE) {
//				tradeOrderType = TradeOrderType.B2C_TRADE_ORDER;
//			} else if (tradeType == TranType.TRANSFER_BANKCARD_BANKCARD || tradeType == TranType.TRANSFER_BANKCARD_BERBON
//					|| tradeType == TranType.TRANSFER_BERBON_BANKCARD || tradeType == TranType.TRANSFER_BERBON_BERBON) {
//				tradeOrderType = TradeOrderType.TRANSFER_ORDER;
//			} else {
//				throw new BusinessException("该支付方式不支持免密支付");
//			}
//			noPwdPayService.recordAccAmount(userId, tradeOrderType, (int) amount);
//		}
//
//		RiskControlRecordDO record = recordDao.getByUser(userId);
//		if (record == null) {
//			record = new RiskControlRecordDO();
//			record.setUserId(userId);
//			record.setCurrentDay(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
//			record.setCurrentMonth(Calendar.getInstance().get(Calendar.MONTH) + 1);
//			recordDao.save(record);
//		} else {
//			if (record.getCurrentDay() != Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
//				record.setTradeDayAmount(0);
//				record.setTradeDayNumber(0);
//				record.setChargeDayAmount(0);
//				record.setChargeDayNumber(0);
//				record.setTransferDayAmount(0);
//				record.setTransferDayNumber(0);
//				record.setWithdrawDayAmount(0);
//				record.setWithdrawDayNumber(0);
//			}
//			if (record.getCurrentMonth() != (Calendar.getInstance().get(Calendar.MONTH) + 1)) {
//				record.setTradeMonthAmount(0);
//				record.setChargeMonthAmount(0);
//				record.setTransferMonthAmount(0);
//				record.setWithdrawMonthAmount(0);
//			}
//			recordDao.update(record);
//		}
//
//		switch (tradeType.getValue()) {
//		case 10:// TranType.CHARGE
//			record.setChargeDayNumber(record.getChargeDayNumber() + 1);
//			record.setChargeDayAmount(record.getChargeDayAmount() + amount);
//			record.setChargeMonthAmount(record.getChargeMonthAmount() + amount);
//			break;
//		case 1:// TranType.PURCHASE
//			record.setTradeDayNumber(record.getTradeDayNumber() + 1);
//			record.setTradeDayAmount(record.getTradeDayAmount() + amount);
//			record.setTradeMonthAmount(record.getTradeMonthAmount() + amount);
//			break;
//		case 30:// TranType.TRANSFER
//		case 31:
//		case 32:
//		case 33:
//			record.setTransferDayNumber(record.getTransferDayNumber() + 1);
//			record.setTransferDayAmount(record.getTransferDayAmount() + amount);
//			record.setTransferMonthAmount(record.getTransferMonthAmount() + amount);
//			break;
//		case 20:// TranType.WITHDRAW
//		case 21:
//		case 22:
//		case 23:
//			record.setWithdrawDayNumber(record.getWithdrawDayNumber() + 1);
//			record.setWithdrawDayAmount(record.getWithdrawDayAmount() + amount);
//			record.setWithdrawMonthAmount(record.getWithdrawMonthAmount() + amount);
//			break;
//		default:
//			break;
//		}
//		recordDao.update(record);
//	}

}
